package gdou.gym.sys.resolve;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import gdou.gym.vo.MyPageInfo;
import gdou.gym.vo.Page;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;



public class PageResolve implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(Page.class);
    }

    @Override
    @SuppressWarnings("null")
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        MyPageInfo<Object> info = new MyPageInfo<>();
        info.setPageNo(webRequest.getParameter(MyPageInfo.PageNum));
        info.setPageSize(webRequest.getParameter(MyPageInfo.PageSize));
        String vo = webRequest.getParameter(MyPageInfo.QueryInfo);
        Object o = null;
        try {
            o = new ObjectMapper().readValue(vo, parameter.getParameterAnnotation(Page.class).clazz());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        info.setQueryInfo(o);
        return info;
    }


}
